import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# 从本地判断图片是否损坏
def is_valid_image(path):
    '''
    检查文件是否损坏
    '''
    try:
        bValid = True
        fileObj = open(path, 'rb')  # 以二进制形式打开
        buf = fileObj.read()
        if not buf.startswith(b'\xff\xd8'):  # 是否以\xff\xd8开头
            bValid = False
        elif buf[6:10] in (b'JFIF', b'Exif'):  # “JFIF”的ASCII码
            if not buf.rstrip(b'\0\r\n').endswith(b'\xff\xd9'):  # 是否以\xff\xd9结尾
                bValid = False
        else:
            try:
                Image.open(fileObj).verify()
            except Exception as e:
                bValid = False
                print(e)
    except Exception as e:
        return False
    return bValid

for folder_name in ("train/positive", "eval/negative", "train/positive", "eval/positive"):
    #os.path.join()连接两个或更多的路径名组件
    folder_path = os.path.join("../dataset/Concrete Crack Images for Classification", folder_name)
    #os.listdir(path)列出该目录下的子目录
    for fname in os.listdir(folder_path):
        fpath = os.path.join(folder_path, fname)
        flag1=is_valid_image(fpath)
        if not flag1:
            print(fpath) #打印错误文件路径及名称

num_skipped = 0
for folder_name in ("train/positive", "eval/negative", "train/positive", "eval/positive"):
    folder_path = os.path.join("../dataset/Concrete Crack Images for Classification", folder_name)
    for fname in os.listdir(folder_path):
        fpath = os.path.join(folder_path, fname)
        try:
            fobj = open(fpath, mode="rb")
            is_jfif = tf.compat.as_bytes("JFIF") in fobj.peek(10)
        finally:
            fobj.close()
        if not is_jfif:
            num_skipped += 1
            # Delete corrupted image
            # os.remove(fpath)
            print(fpath) #打印错误文件路径及名称

print("Error %d images" % num_skipped)